<template>
    <div class="register-wrap">
        <div class="ms-register">
            <div class="ms-title">New User Mini Blog !</div>
            <el-form :model="param" :rules="rules" ref="register" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="param.username" placeholder="昵称">
                        <template #prepend>
                            <el-button icon="el-icon-user"></el-button>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="tel">
                    <el-input v-model="param.tel" placeholder="手机号">
                        <template #prepend>
                            <el-button icon="el-icon-phone-outline"></el-button>
                        </template>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                        type="password"
                        placeholder="登录口令"
                        v-model="param.password"
                        @keyup.enter="submitFormRegister()"
                    >
                        <template #prepend>
                            <el-button icon="el-icon-lock"></el-button>
                        </template>
                    </el-input>
                </el-form-item>
                <div class="register-btn">
                    <el-button type="warning" @click="submitFormRegister()">注册</el-button>
                </div>
                <div class="register-btn">
                    <el-button type="primary" @click="submitFormLogin()">登录</el-button>
                </div>
                
                <p class="register-tips">Tips : 登录之后，获取更好的浏览体验</p>
            </el-form>
        </div>
    </div>
</template>

<script>
import _global from './Global.vue'
export default {
    data() {
        return {
            param: {
                username: "",
                tel: "",
                password: ""
            },
            rules: {
                username: [
                    { required: true, message: "请输入用户名【4-16位标准字符】", trigger: "blur" }
                ],
                tel: [
                    { required: true, message: "请输入手机号", trigger: "blur" }
                ],
                password: [
                    { required: true, message: "请输入密码【6-16位标准字符】", trigger: "blur" }
                ]
            }
        };
    },
    created() {
        this.$store.commit("clearTags");
    },
    methods: {
        submitFormRegister() {
            this.$refs.register.validate(valid => {
                if (valid) {
                    fetch(`http://${_global.request_host}:${_global.reuqest_port}/user/register/`, {
                    method:'POST',
                    mode: 'cors',
                    headers:{
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    body:`name=${this.param.username}&tel=${this.param.username}&pwd=${this.param.password}&tel_code=888888`,
                    })
                    .then(resp => resp.json())
                    .then(json => {
                        if(json.code==200){
                            localStorage.setItem('token', json.results.token)
                            localStorage.setItem('ms_username', json.results.username)
                            localStorage.setItem('user_image_path', json.results.user_image_path)
                            this.$message({
                                message: json.results.message,
                                type: 'success',
                                center: true,
                            });
                            this.$router.push("/");
                        }else{
                            this.$message({
                                message: json.results.message,
                                type: 'warning',
                                center: true,
                            });
                        }
                    })
                } else {
                    this.$message.error("请输入账号和密码");
                    return false;
                }
            });
        },
        submitFormLogin() {
            this.$router.push('/login')
        }
    }
};
</script>

<style scoped>
.register-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../assets/img/login-bg.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: #fff;
    border-bottom: 1px solid #ddd;
}
.ms-register {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.3);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.register-btn {
    text-align: center;
}
.register-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
.register-tips {
    font-size: 12px;
    line-height: 30px;
    color: #fff;
}
</style>